package top.dancun.easuser.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.dancun.common.utils.R;
import top.dancun.easuser.entity.MenuEntity;
import top.dancun.easuser.service.MenuService;
import top.dancun.easuser.service.ShiroService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 
 *
 * @author dancun
 * @email message@dancun.top
 * @date 2022-09-05 22:15:48
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends AbstractController{
    @Autowired
    private MenuService menuService;

    @Autowired
    private ShiroService shiroService;


    /**
     * 导航
     */
    @RequestMapping("/nav")
    public R list(){
        List<MenuEntity> menuList = menuService.getUserMenuList(getUserId());
        Set<String> permissions = shiroService.getUserPermissions(getUserId());

        return R.ok().put("menuList", menuList).put("permissions",permissions);
    }

    /**
     * 列表
     */
    @RequestMapping("/listtree")
    @RequiresPermissions("easuser:menu:list")
    public R listTree(@RequestParam Map<String, Object> params){
        List<MenuEntity> menus = menuService.getMenuAndPermissList();

        return R.ok().put("data", menus);
    }

    @RequestMapping("/list")
    @RequiresPermissions("easuser:menu:list")
    public R list(@RequestParam Map<String, Object> params){
        List<MenuEntity> menus = menuService.list();

        return R.ok().put("data", menus);
    }

    @RequestMapping("/selected")
    @RequiresPermissions("easuser:menu:list")
    public R selected(@RequestParam Map<String, Object> params){
        List<MenuEntity> menus = menuService.getMenuList();

        return R.ok().put("data", menus);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{menuId}")
    @RequiresPermissions("easuser:menu:info")
    public R info(@PathVariable("menuId") Long menuId){
		MenuEntity menu = menuService.getById(menuId);

        return R.ok().put("menu", menu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("easuser:menu:save")
    public R save(@RequestBody MenuEntity menu){
		menuService.save(menu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("easuser:menu:update")
    public R update(@RequestBody MenuEntity menu){
		menuService.updateById(menu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("easuser:menu:delete")
    public R delete(@RequestBody Long[] menuIds){
		menuService.removeByIds(Arrays.asList(menuIds));

        return R.ok();
    }

}
